package com.leenmvc.core.dao.sqlServer.wrapper;

import com.leenmvc.core.dao.communal.WrapperFactory;
import com.leenmvc.core.utils.ClassUtil;
import com.leenmvc.core.utils.thread.ThreadLocalManager;
import com.leenmvc.core.dao.communal.Wrapper;

import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 这个类用来管理实际使用的wrapper类
 * 使用池技术， 固定Wrapper的数量以达到节约内存的作用
 */
public class SqlServerWrapperFactory implements WrapperFactory {

    /**
     * 测试用，项目结束了将本变量去除
     */
    private static boolean test_wrapper = false;

    private static Queue<SqlServerQueryWrapper> queryWrappers = new ConcurrentLinkedQueue();

    private static Queue<SqlServerDeleteWrapper> deleteWrappers = new ConcurrentLinkedQueue();

    private static Queue<SqlServerUpdateWrapper> updateWrappers = new ConcurrentLinkedQueue();

    private static Queue<SqlServerCountWrapper> countWrappers = new ConcurrentLinkedQueue();

    static {
        for (int i = 0; i < 100; i++) {
            queryWrappers.add(new SqlServerQueryWrapper());
        }
        for (int i = 0; i < 50; i++) {
            deleteWrappers.add(new SqlServerDeleteWrapper());
            updateWrappers.add(new SqlServerUpdateWrapper());
            countWrappers.add(new SqlServerCountWrapper());
        }

    }

    /**
     * 获取可用的countWrapper
     *
     * @return
     */
    public <T> T getCountWrapper(Class<?> cls) {
        SqlServerCountWrapper wrapper = null;
        if(test_wrapper) {
            System.out.println("countWrappers的数量是：" + countWrappers.size());
        }
        if (countWrappers.size() > 0) {
            wrapper = countWrappers.poll();
            ThreadLocalManager.addWrapper(wrapper);
            wrapper.setCls(cls);
            wrapper.setOutColumns(ClassUtil.getFillFieldColumnList(cls));
            wrapper.setColumns(ClassUtil.getColumns(cls));
            wrapper.setFields(ClassUtil.getFields(cls));
        }
        if(test_wrapper) {
            if(test_wrapper) {
            System.out.println("countWrappers的数量是：" + countWrappers.size());
        }
        }
        return (T)wrapper;
    }

    /**
     * 获取可用的wrapper
     *
     * @return
     */
    public  <T> T getQueryWrapper(Class<?> cls) {
        SqlServerQueryWrapper wrapper = null;
        if(test_wrapper) {
            System.out.println("queryWrappers的数量是：" + queryWrappers.size());
        }
        if (queryWrappers.size() > 0) {
            wrapper = queryWrappers.poll();
            ThreadLocalManager.addWrapper(wrapper);
            wrapper.setCls(cls);
            wrapper.setOutColumns(ClassUtil.getFillFieldColumnList(cls));
            wrapper.setColumns(ClassUtil.getColumns(cls));
            wrapper.setFields(ClassUtil.getFields(cls));
        }
        if(test_wrapper) {
            System.out.println("queryWrappers的数量是：" + queryWrappers.size());
        }
        return (T)wrapper;
    }

    public <T> T getQueryWrapper(Class<?> cls, Map<String,Object> params) {
        SqlServerQueryWrapper wrapper = null;
        if(test_wrapper) {
            System.out.println("queryWrappers的数量是：" + queryWrappers.size());
        }
        if (queryWrappers.size() > 0) {
            wrapper = queryWrappers.poll();
            ThreadLocalManager.addWrapper(wrapper);
            wrapper.setCls(cls);
            wrapper.setOutColumns(ClassUtil.getFillFieldColumnList(cls));
            wrapper.setColumns(ClassUtil.getColumns(cls));
            wrapper.setFields(ClassUtil.getFields(cls));
            wrapper.mapToWrapper(params);
        }
        if(test_wrapper) {
            System.out.println("queryWrappers的数量是：" + queryWrappers.size());
        }
        return (T)wrapper;

    }

    /**
     * 获取可用的wrapper
     *
     * @return
     */
    public <T, K> T getUpdateWrapper(K t) {
        SqlServerUpdateWrapper wrapper = null;
        if(test_wrapper) {
            System.out.println("updateWrappers的数量是：" + updateWrappers.size());
        }
        if (updateWrappers.size() > 0) {
            wrapper = updateWrappers.poll();
            ThreadLocalManager.addWrapper(wrapper);
            wrapper.update(t);
        }
        if(test_wrapper) {
            System.out.println("updateWrappers的数量是：" + updateWrappers.size());
        }
        return (T)wrapper;
    }

    /**
     * 获取可用的wrapper
     *
     * @return
     */
    public <T> T getUpdateWrapper(Class<?> cls) {
        SqlServerUpdateWrapper wrapper = null;
        if(test_wrapper) {
            System.out.println("updateWrappers的数量是：" + updateWrappers.size());
        }
        if (updateWrappers.size() > 0) {
            wrapper = updateWrappers.poll();
            ThreadLocalManager.addWrapper(wrapper);
            wrapper.setCls(cls);
        }
        if(test_wrapper) {
            System.out.println("updateWrappers的数量是：" + updateWrappers.size());
        }
        return (T)wrapper;
    }

    /**
     * 获取可用的wrapper
     *
     * @return
     */
    public <T> T getUpdateWrapper(Class<?> cls, Map<String, Object> params) {
        SqlServerUpdateWrapper wrapper = null;
        if(test_wrapper) {
            System.out.println("updateWrappers的数量是：" + updateWrappers.size());
        }
        if (updateWrappers.size() > 0) {
            wrapper = updateWrappers.poll();
            ThreadLocalManager.addWrapper(wrapper);
            wrapper.update(cls, params);
        }
        if(test_wrapper) {
            System.out.println("updateWrappers的数量是：" + updateWrappers.size());
        }
        return (T)wrapper;
    }

    /**
     * 获取可用的wrapper
     *
     * @return
     */
    public <T> T getDeleteWrapper(Class<?> cls) {
        SqlServerDeleteWrapper wrapper = null;
        if(test_wrapper) {
            System.out.println("deleteWrappers 的数量是：" + deleteWrappers.size());
        }
        if (deleteWrappers.size() > 0) {
            wrapper = deleteWrappers.poll();
            ThreadLocalManager.addWrapper(wrapper);
            wrapper.setCls(cls);
        }
        if(test_wrapper) {
            System.out.println("deleteWrappers 的数量是：" + deleteWrappers.size());
        }
        return (T)wrapper;
    }

    /**
     * 关闭wrapper
     *
     * @param wrapper
     */
    public void closeWrapper(Wrapper wrapper) {
        closeWrapper(wrapper, false);
        ThreadLocalManager.removeWrapper(wrapper);
    }

    /**
     * ThreadLocalManager专用
     *
     * @param wrapper
     */
    public void closeWrapper(Wrapper wrapper, boolean threadLocalM) {
        if (wrapper instanceof SqlServerQueryWrapper) closeWrapper((SqlServerQueryWrapper) wrapper);
        if (wrapper instanceof SqlServerUpdateWrapper) closeWrapper((SqlServerUpdateWrapper) wrapper);
        if (wrapper instanceof SqlServerDeleteWrapper) closeWrapper((SqlServerDeleteWrapper) wrapper);
        if (wrapper instanceof SqlServerCountWrapper) closeWrapper((SqlServerCountWrapper) wrapper);
    }

    /**
     * 关闭在用的wrapper
     *
     * @param wrapper
     */
    private static void closeWrapper(SqlServerQueryWrapper wrapper) {
        wrapper.clear();
        queryWrappers.add(wrapper);
    }

    /*
     * 关闭在用的wrapper
     *
     * @param wrapper
     */
    private static void closeWrapper(SqlServerUpdateWrapper wrapper) {
        wrapper.clear();
        updateWrappers.add(wrapper);
    }

    /*
     * 关闭在用的wrapper
     *
     * @param wrapper
     */
    private static void closeWrapper(SqlServerDeleteWrapper wrapper) {
        wrapper.clear();
        deleteWrappers.add(wrapper);
    }

    /*
     * 关闭在用的wrapper
     *
     * @param wrapper
     */
    private static void closeWrapper(SqlServerCountWrapper wrapper) {
//        LogFactory.log(Level.INFO,"查询 CountWrapper 关闭前：" + countWrappers.size(), LogType.WRAPPER);
        wrapper.clear();
        countWrappers.add(wrapper);
    }

}
